<?php

class Default_Model_User
{
    
    protected $_username;
    protected $_password;
    protected $_id;
    protected $_udata;
    protected $_table;
    protected $_db;

    public function __construct (array $user)
    {
        /* Khởi tạo đối tượng */
        $this->_table = new Application_Model_DbTable_Khachhang();
        $this->_username = $user[0];
        $this->_password = $user[1];
        $this->_db = Zend_Registry::get('db');
    }

    public function valid ()
    {
        /* Kiểm tra trong csdl có tồn tại user không ?
         * Nếu tồn tại thì trả ra một đối tượng Zend_Db_Table_Row_Abstract, ngược lại trả ra null
         * */
        $row = $this->_table->fetchRow(
                $this->_table->select()
                    ->where('username = :name and password = :pw')
                    ->bind(
                        array(
                                ':name' => $this->_username,
                                ':pw' => $this->_password
                        )));
        if(!is_null($row))
        {
            $this->_udata = $row;
        	$this->_id = $row->id;
        	return true;
        }
        return false;
    }
    
    public function data(){
    	if(isset($this->_udata))
    	    return $this->_udata;
        return false;
    }
    
    public function like($id)
    {
        /* Người dùng like sản phẩm
         * 
         * */
        if(isset($this->_id))
        {
            $sql=<<<EOD
                INSERT INTO tbl_customer_like (id_cus, id_sp)
                SELECT * FROM (SELECT :cus, :sp) AS tmp
                WHERE NOT EXISTS (
                    SELECT * FROM tbl_customer_like WHERE id_cus = :cus and id_sp = :sp
                ) LIMIT 1
EOD;
            $product = new Default_Model_Product();
            $item = $product->id($id)->current();
            if(!is_null($item))
            {
                $this->_db->query($sql,array(':cus' => $this->_id,':sp' => $id));
                $item->like++;
                $item->save();
                return true;
            }
        }
        return false;
    }
    
    public function dislike($id)
    {
    	/* Người dùng dislike sản phẩm
    	 * 
    	 * */
        $sql=<<<EOD
        DELETE FROM `tbl_customer_like` WHERE `tbl_customer_like`.`id_sp` = :id and `tbl_customer_like`.`id_cus` = :cus
EOD;
        if(isset($this->_id))
        {
            $product = new Default_Model_Product();
            $item = $product->id($id)->current();
            if(!is_null($item))
            {
                $this->_db->query($sql,array(':id' => $id,':cus'=>$this->_id));
                $item->like--;
                $item->save();
                return true;
            }
        }
        return false;
    }
    
    public function likes()
    {
    	$like = new Application_Model_DbTable_Like();
    	return $like->fetchAll(
        	        $like->select()
        	              ->where('id_cus = ?', "{$this->_id}")
    	        );
    }
    
    public function history()
    {
    	/* Lich sử mua hàng của khách hàng
    	 * */
        $sql =<<<EOD
            SELECT `d`.`name` ,`a`.`success`, `b`.`id_hoa_don` , `a`.`ngay_lap_hoa_don` , `b`.`so_luong` , sum( `b`.`so_luong` * `c`.`gia_ban` ) AS `total` , sum( `b`.`so_luong` ) AS `soluong`
            FROM `tbl_hoa_don_ban` AS `a`
            INNER JOIN `tbl_hoa_don_sp` AS `b` ON `a`.`id_hoa_don` = `b`.`id_hoa_don`
            INNER JOIN `tbl_products` AS `c` ON `b`.`id_sp` = `c`.`id`
            INNER JOIN `tbl_customers` AS d ON `a`.`id_khach_hang` = `d`.`id`
            WHERE `id_khach_hang` = :cus
            GROUP BY `b`.`id_hoa_don`
EOD;
        $tmp = array();
        if(isset($this->_id))
        {
            $tmp = $this->_db->query($sql,array(':cus'=>$this->_id))->fetchAll();
        }
        return $tmp;
        
    }
    
    public function order($id)
    {
    	/* Xem chi tiết hóa đơn
    	 * 
    	 * */
        $tmp = array();
        if(isset($this->_id))
        {
            $sql =<<<EOD
            SELECT `d`.`name` , `b`.`id_hoa_don` ,`a`.`success`, `a`.`ngay_lap_hoa_don` , `b`.`so_luong` , sum( `b`.`so_luong` * `c`.`gia_ban` ) AS `total` , sum( `b`.`so_luong` ) AS `soluong`
            FROM `tbl_hoa_don_ban` AS `a`
            INNER JOIN `tbl_hoa_don_sp` AS `b` ON `a`.`id_hoa_don` = `b`.`id_hoa_don`
            INNER JOIN `tbl_products` AS `c` ON `b`.`id_sp` = `c`.`id`
            INNER JOIN `tbl_customers` AS d ON `a`.`id_khach_hang` = `d`.`id`
            WHERE `id_khach_hang` = :cus and `a`.`id_hoa_don`= :id
            GROUP BY `b`.`id_hoa_don`
EOD;
            
            $data = array(
                 ':cus'=> $this->_id,
                 ':id' => $id
            );
        	$result = $this->_db->query($sql,$data)->fetchAll();
        	$tmp[] = $result[0];
        	$sql1=<<<EOD
        	SELECT `c`. *,`b`.`so_luong`
            FROM `tbl_hoa_don_ban` AS `a`
            INNER JOIN `tbl_hoa_don_sp` AS `b` ON `a`.`id_hoa_don` = `b`.`id_hoa_don`
            INNER JOIN `tbl_products` AS `c` ON `b`.`id_sp` = `c`.`id`
            WHERE `a`.`id_khach_hang` = :cus and `a`.`id_hoa_don` = :id
EOD;
        	$tmp[]=$this->_db->query($sql1,$data)->fetchAll();
        }
        return $tmp;
        
    }
}

